package com.sunnos.express.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunnos.express.entity.Customer;
import com.sunnos.express.service.CustomerService;

@Controller
@RequestMapping("/customer")
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	
	/**
	 * ��ѯ��ɫ�б�
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/listAll")
	public @ResponseBody Map<String,Object> listAll(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			int totalCount = customerService.getCustomerListTotal(param);
			List<Customer> ret = customerService.getCustomerList(param);
			responseMap.put("rows", ret);
			responseMap.put("totalCount", totalCount);
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ѯ��ɫ�б�
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/list")
	public @ResponseBody Map<String,Object> list(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String start2 = request.getParameter("start");//��ǰҳ��һ����ݵ�����
		String limit = request.getParameter("limit");//ÿҳ����
		int start = Integer.parseInt(start2);
		int pageSize = Integer.parseInt(limit);
		//int pageNo = start/pageSize+1;
		int end = start+pageSize;
		String contactName = request.getParameter("contactname");
		String name = request.getParameter("name");
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("contactName", contactName);
			param.put("customerName", name);
			param.put("offset", start);
			param.put("limitSize", end);
			int totalCount = customerService.getCustomerListTotal(param);
			List<Customer> ret = customerService.getCustomerList(param);
			responseMap.put("rows", ret);
			responseMap.put("totalCount", totalCount);
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("success", "false");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}	

	/**
	 * ����ɫ���Ƿ�ע��
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/checkName",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> checkAccount(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		//System.out.println("check name return paramer is .............."+id+","+name);
		String ret = "failure";
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			if (null==id || "".equals(id) || "null".equals(id)) {
				if (null!=name && !"".equals(name)) {
					param.put("id", "");
					param.put("customerName", name);
					Customer customer = customerService.getCustomer(param);		
					if(customer!=null) {
						ret = "ok";
					}else {
						ret = "failure";
					}
				}
			}
			else {
				if (null!=name && !"".equals(name)) {
					param.put("customerName", name);
					param.put("id", "");
					Customer customer = customerService.getCustomer(param);			
					if(customer!=null) {
						param.put("id", id);
						param.put("customerName", "");
						Customer oraginCustomer = customerService.getCustomer(param);
						if (oraginCustomer!=null) {
							if (oraginCustomer.getCustomerName().equals(customer.getCustomerName())) {
								ret = "failure";
							}
							else {
								ret = "ok";
							}
						}
					}else {
						ret = "failure";
					}
				}
			}
			responseMap.put("msg", ret);
			responseMap.put("info", "�ͻ���ƴ���");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ���ָ����ɫ����
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/getCustomer",method=RequestMethod.POST)
	public @ResponseBody Map<String,Object> getRole(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,Object> responseMap = new HashMap<String,Object>();
		String id = request.getParameter("id");
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("id", id);
			Customer customer = customerService.getCustomer(param);
			responseMap.put("msg", "ok");
			responseMap.put("customer", customer);
			responseMap.put("info", "��ȡָ���ͻ�����ɹ�");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("info", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ɾ���ɫ
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody Map<String,String> delete(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,String> responseMap = new HashMap<String,String>();
		String id = request.getParameter("id");		
		try {
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("ids", id);
			customerService.deleteCustomer(param);
			responseMap.put("msg", "ok");
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("success", "false");
			responseMap.put("errors", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
	
	/**
	 * ��ӻ��޸��û�
	 * @param request
	 * @param response
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public @ResponseBody Map<String,String> save(HttpServletRequest request,   
			HttpServletResponse response){
		//spring������jackson�Զ�������ֵ��װ��JSON����
		Map<String,String> responseMap = new HashMap<String,String>();
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String contactName = request.getParameter("contactName");
		String contactPhone = request.getParameter("contactPhone");
		String contactFax = request.getParameter("contactFax");
		String address = request.getParameter("address");
		String memo = request.getParameter("memo");
		try {
			if (id==null || "".equals(id) || "null".equals(id)) {
				Customer customer = new Customer();
//				customer.setAddress(address);
//				customer.setContactFax(contactFax);
//				customer.setContactName(contactName);
//				customer.setContactPhone(contactPhone);
				customer.setMemo(memo);
				customer.setCustomerName(name);
				customerService.insertCustomer(customer);
			}
			else {
				Customer customer = new Customer();
//				customer.setAddress(address);
//				customer.setContactFax(contactFax);
//				customer.setContactName(contactName);
//				customer.setContactPhone(contactPhone);
//				customer.setMemo(memo);
//				customer.setCustomerName(name);
//				customer.setId(Integer.parseInt(id));
				customerService.updateCustomer(customer);			
			}
			responseMap.put("msg", "ok");
			responseMap.put("success", "true");
			return responseMap;
		}catch(Exception e) {
			e.printStackTrace();
			responseMap.put("msg", "failure");
			responseMap.put("success", "false");
			responseMap.put("errors", e.getClass()+":"+e.getMessage());
			return responseMap;
		}
	}
}
